package com.myCommunity.ui;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myCommunity.dao.impl.UsersDaoImpl;
import com.myCommunity.entity.Users;

public class RegisterServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String nickname = request.getParameter("nickname");
		String email = request.getParameter("email");
		Users user = new UsersDaoImpl().fetchUsername(username);
		if(user!=null){
			request.setAttribute("error", "用户名已被注册，请重新指定");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}else{
			Users u = new Users(0, username, nickname, password, null, email, 101, 1, 1,null);
			new UsersDaoImpl().add(u);
			response.sendRedirect("login.jsp");
		}
		
	}

}
